﻿use strict;
use utf8;
use Unicode::Japanese;

my $RES = "res.txt";
my $RES_COMPARE = "res_compare.txt";
my @res_compare;

main();
exit;

#-----------------------------------------------------------
# メイン処理。
#-----------------------------------------------------------
sub main {
	my @res;

	$RES = add_date($RES);
	$RES_COMPARE = add_date($RES_COMPARE);
	chdir "../../..";
	`cvs update -d`;
	if ($? != 0) {
		error("error: cvs update: $?\n");
	}

	chdir "translations/pm_eme_tools/xls2dat";
	@res = `xls2dat.pl english list.txt`;
	if ($? != 0) {
		error("error: cvs update: $?\n");
	}

	check("res");

	open RES, ">$RES" or die "error: open $RES\n";
	print RES @res;
	close RES;
	open RES_COMPARE, ">$RES_COMPARE" or die "error: open $RES_COMPARE\n";
	print RES_COMPARE @res_compare;
	close RES_COMPARE;
}

#-----------------------------------------------------------
# 結果フォルダを再帰処理。
#-----------------------------------------------------------
sub check {
	my ($path) = @_;
	my @list;

	@list = glob("$path/*");
	for my $i (@list) {
		if (-f $i) {
			compare_real_with($i);
		} elsif (-d $i) {
			check($i);
		}
	}
}

#-----------------------------------------------------------
# エラー出力。
#-----------------------------------------------------------
sub error {
	my ($msg) = @_;

	print $msg;
	exit -1;
}

#-----------------------------------------------------------
# 結果比較。
#-----------------------------------------------------------
sub compare_real_with {
	my ($src) = @_;
	my $dst;

	if ($src =~ /res\/(.*)$/) {
		$dst = "../../../$1";
		push @res_compare, "$src\n";
		push @res_compare, `diff $src $dst`;
	}
}

#-----------------------------------------------------------
# ファイル名に日付付加。
#-----------------------------------------------------------
sub add_date {
	my ($src) = @_;
	my ($mon, $day);

	(undef, undef, undef, $day, $mon) = localtime;

	return sprintf("%02d%02d$src", $mon, $day);
}
